package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityMovieDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class MovieDetailActivity extends BaseAc<ActivityMovieDetailBinding> {

    public static StkResBean stkResBean;
    private boolean isCollect;
    private List<StkResBean> listSp;

    @Override
    protected int onCreate() {
        return R.layout.activity_movie_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivMovieDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivMovieCollect.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivMovieCollect://收藏
                if (isCollect) {
                    listSp.remove(stkResBean);
                    AppSpUtils.saveCollectList(listSp);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(stkResBean);
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        AppSpUtils.saveCollectList(listSave);
                    }
                }
                setCollectData();
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.webView.loadUrl(stkResBean.getUrl());
        setCollectData();
    }

    //收藏
    private void setCollectData() {
        listSp = AppSpUtils.getCollectList();
        isCollect = false;
        mDataBinding.ivMovieCollect.setImageResource(R.drawable.wsc);
        if (listSp != null && listSp.size() != 0) {
            for (StkResBean bean : listSp) {
                if (bean.getUrl().equals(stkResBean.getUrl())) {
                    isCollect = true;
                    mDataBinding.ivMovieCollect.setImageResource(R.drawable.sc);
                    break;
                }
            }
        }
    }

}